package com.qdlc.p2p.biz.borrowworker.worker;

import org.apache.log4j.Logger;

import com.qdlc.p2p.common.constant.BorrowStatus;
import com.qdlc.p2p.common.exception.BorrowException;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dao.BorrowRepaymentDao;
import com.qdlc.p2p.dal.dao.UserCreditDao;
import com.qdlc.p2p.dal.dao.UserCreditLogDao;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.BorrowConfig;
import com.qdlc.p2p.dal.dto.BorrowRepayment;
import com.qdlc.p2p.dal.dto.BorrowTender;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCredit;
import com.qdlc.p2p.dal.dto.UserCreditLog;
import com.qdlc.p2p.dal.model.BorrowModel;

/**
 * 信用标
 * 
 * @author fuxingxing
 * @date 2012-9-5 下午5:18:52
 * @version x
 */
public class CreditBorrowWorker extends BaseBorrowWorker {

	private static Logger logger = Logger.getLogger(CreditBorrowWorker.class);

	@SuppressWarnings("unused")
	private static final long serialVersionUID = 6478298326297026207L;

	public CreditBorrowWorker(Borrow data, BorrowConfig config, boolean flag) {
		super(data, config, flag);
	}

	/**
	 * 借款标跳过复审，复审成功
	 */
	@Override
	public void immediateRepayAfterTender(BorrowTender tender) {
		super.immediateInterestAfterTender(tender);
		if(this.data.getAccountYes() >= this.data.getAccount()){
			super.skipReview(); //满标如果跳过复审将状态改为3
			if (this.data.getStatus() == BorrowStatus.STATUS_RECHECK_PASS) {
				BorrowModel model = BorrowModel.instance(this.data);
				// 去掉,暂时没有使用
//				BorrowFullVerifyService borrowFullVerifyService = (BorrowFullVerifyService)BeanUtil
//						.getBean("borrowFullVerifyService");
//				borrowFullVerifyService.processVerifyFullSuccess(model);
			}
		}
	}
	
	/**
	 * 撤标
	 */
	@Override
	public void revokeBorrow() {
		super.revokeBorrow();
		double amount = this.data.getAccount();
		UserCreditDao userCreditDao = (UserCreditDao) BeanUtil.getBean("userCreditDao");
		UserCreditLogDao userCreditLogDao = (UserCreditLogDao) BeanUtil.getBean("userCreditLogDao");
		UserCredit ua = userCreditDao.findObjByProperty("user.userId", this.data.getUser().getUserId());
		if (ua == null) {
			throw new BorrowException("用户" + this.data.getUser().getUserId() + "的信用账户不存在.", 1);
		}
		userCreditDao.update(0, amount, -amount, ua.getUser().getUserId());
		UserCreditLog amountLog = new UserCreditLog();
		amountLog.setUser(ua.getUser());
		amountLog.setType("borrow_cancel");
		amountLog.setAccount(amount);
		amountLog.setAccountAll(ua.getCreditUse());
		amountLog.setAccountUse(ua.getCreditUse() + amount);
		amountLog.setAccountNoUse(ua.getCreditNouse() - amount);
		amountLog.setRemark("用户撤回借款标");
		userCreditLogDao.save(amountLog);
	}

	@Override
	public boolean allowPublish(User user) {
		super.allowPublish(user);
		UserCreditDao uad = (UserCreditDao) BeanUtil.getBean("userCreditDao");
		UserCredit amount = uad.findByUserId(user.getUserId());
		if (amount == null || data.getAccount() > amount.getCreditUse()) {
			throw new BorrowException("可用信用额度不足!", 1);
		}
		return true;
	}

	@Override
	public Borrow handleBorrowAfterPublish(Borrow borrow) {
		update(borrow);
		return borrow;
	}

	private void update(Borrow borrow) {
		UserCreditDao userCreditDao = (UserCreditDao) BeanUtil.getBean("userCreditDao");
		UserCreditLogDao userCreditLogDao = (UserCreditLogDao) BeanUtil.getBean("userCreditLogDao");
		UserCredit ua = userCreditDao.findObjByProperty("user.userId", borrow.getUser().getUserId());
		double amount = borrow.getAccount();
		userCreditDao.update(0, -amount, amount, borrow.getUser().getUserId());
		UserCreditLog amountLog = new UserCreditLog();
		amountLog.setUser(ua.getUser());
		amountLog.setAccount(amount);
		amountLog.setAccountAll(ua.getCredit());
		amountLog.setAccountUse(ua.getCreditUse() - amount);
		amountLog.setAccountNoUse(ua.getCreditNouse() + amount);
		amountLog.setType("");
		amountLog.setRemark("扣除");
		userCreditLogDao.save(amountLog);
	}

	@Override
	public void borrowRepayHandleBorrow(BorrowRepayment repay) {
		super.borrowRepayHandleBorrow(repay);
		// 如果是信用标，将本次的借款金额累加回借款人的信用额度，生成信用额度流水
		UserCreditDao userCreditDao = (UserCreditDao) BeanUtil.getBean("userCreditDao");
		UserCredit amount = userCreditDao.findObjByProperty("user.userId", data.getUser().getUserId());
		userCreditDao.update(0, repay.getCapital(), -repay.getCapital(), amount.getUser().getUserId());
	}
	
	@Override
	public void borrowPriorRepayHandleBorrow(BorrowRepayment repay) {
		super.borrowPriorRepayHandleBorrow(repay);
		// 如果是信用标，借款金额累加回借款人的信用额度
		UserCreditDao userCreditDao = (UserCreditDao) BeanUtil.getBean("userCreditDao");
		BorrowRepaymentDao borrowRepaymentDao = (BorrowRepaymentDao) BeanUtil.getBean("borrowRepaymentDao");
		UserCredit amount = userCreditDao.findObjByProperty("user.userId", data.getUser().getUserId());
		double waitRemainderRepayCapital = borrowRepaymentDao.getRemainderCapital(repay.getBorrow().getId()); //计算剩余待还本金
		userCreditDao.update(0, waitRemainderRepayCapital, -waitRemainderRepayCapital, amount.getUser().getUserId());
	}

	/**
	 * 撤标
	 */
	@Override
	public void stopBorrow() {
		super.stopBorrow();
		UserCreditDao userCreditDao = (UserCreditDao) BeanUtil.getBean("userCreditDao");
		UserCreditLogDao userCreditLogDao = (UserCreditLogDao) BeanUtil.getBean("userCreditLogDao");
		UserCredit ua = userCreditDao.findObjByProperty("user.userId", this.data.getUser().getUserId());
		if (ua == null) {
			throw new BorrowException("用户" + this.data.getUser().getUserId() + "的信用账户不存在.", 1);
		}
		double account = this.data.getOldAccount();
		double account_yes = this.data.getAccountYes();
		double amountBack = account - account_yes;
		userCreditDao.update(0, amountBack, -amountBack, ua.getUser().getUserId());
		UserCreditLog amountLog = new UserCreditLog();
		amountLog.setType("borrow_cancel");
		amountLog.setAccount(amountBack);
		amountLog.setAccountAll(ua.getCreditUse());
		amountLog.setAccountUse(ua.getCreditUse() + amountBack);
		amountLog.setAccountNoUse(ua.getCreditNouse() - amountBack);
		amountLog.setRemark("截标退回多占用的信用额度");
		amountLog.setAddTime(DateUtil.getNow());
		userCreditLogDao.save(amountLog);
	}
}
